package com.zhipu.balance.ui.vm.home

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.zhipu.balance.common.Constants
import com.zhipu.balance.listener.SerialPortListener
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.GeneralBalanceActivity
import com.zhipu.balance.ui.home.HomeActivity
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.OpenSerialPort
import com.zhipu.balance.utils.data.DisposeUtils
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit
import kotlin.experimental.and


/**
 * 普通天平
 */
class GeneralBalanceModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService),SerialPortListener{

    var weighing = ObservableField("0.00")

    init {
        OpenSerialPort.onSerialPortListener(this)
    }



    fun FunctionalBalance(){
        ActivityUtils.startActivity(HomeActivity::class.java)
        ActivityUtils.finishActivity(GeneralBalanceActivity::class.java)
    }


    /**
     * 每隔milliseconds毫秒后执行next操作
     */
    fun milliseconds(){
        Observable.interval(100, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(object : Observer<Long>{

            override fun onSubscribe(d: Disposable) {
            }

            override fun onNext(t: Long) {
                /**
                 * 重量輸出命令
                 */
                OpenSerialPort.output()
            }

            override fun onError(e: Throwable) {
            }

            override fun onComplete() {
            }

        })
    }





    override fun itemClicked(event: ByteArray?) {
        /**
         * 数值
         */
        val numericalValue = ConvertUtils.bytes2HexString(event)
        if (event!!.size >= 10) {
            when (numericalValue) {
                Constants.SEND_GO_START -> {
                    println("去皮成功")
                }
                Constants.FARMAR_500 -> {
                    weighing.set("砝码500")
                }
                Constants.FARMAR_1000 -> {
                    weighing.set("砝码1000")
                }
                Constants.FARMAR_2000 -> {
                    weighing.set("砝码2000")
                }
                Constants.FARMAR_3000 -> {
                    weighing.set("砝码3000")
                }
                Constants.CALIBRATION_500 -> {
                    //串口发送十六进制 33 0D 0A  标定500G 标定完成重新上电就好了
                    weighing.set("完成定标")
                }
                Constants.OVERLOAD -> {
                    weighing.set("超重")
                }

                Constants.SHIELD -> {
                    weighing.set("校准中")
                }
                else -> {
                    weighing.set(DisposeUtils.balanceCorrection(event))
                }
            }
        }
    }



}